package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.common.CommonReslut;
import com.neusoft.entity.Student;
import com.neusoft.excelUtils.ExcelForList;
import com.neusoft.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2020-06-01 20:49:57
 */
@Controller
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Student selectOne(Integer id) {
        return this.studentService.queryById(id);
    }

    //管理查询界面
    @RequestMapping("tolist")
    public String tolist() {
        return "/user/student_table";
    }
    //admin页面的查询所有功能
    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(@RequestParam("page") Integer page, @RequestParam("limit")Integer limit, @RequestBody Student student){

        CommonReslut<Student> result=new CommonReslut<>();
        IPage<Student> iPage =studentService.queryAllByLimit(page,limit,student);
        result.setCode(0);
        System.out.println("iPage.getTotal():::"+iPage.getTotal());
        System.out.println("iPage.getRecords():::"+iPage.getRecords());
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;
    }

    //管理添加界面
    @RequestMapping("toadd")
    public String toadd(Model model) {
        model.addAttribute("student",new Student());
        return "/user/student_add";
    }

    //管理添加保存功能
    @PostMapping("save")
    @ResponseBody
    public Object save(@RequestBody Student student) {
        boolean result=false;
        System.out.println("student:::"+student);

        if(student.getId()!=null){
            //编辑
            result =studentService.update(student)>0;
        }else{
            //添加
            result=studentService.insert(student)>0;
        }


        return result;
    }
    //管理更新功能
    @RequestMapping("toedit")
    public String toedit(int id,Model model) {

        System.out.println("id:::"+id);
        Student student=studentService.queryById(id);
        model.addAttribute("student",student);

        return "user/student_add";
    }

    //管理删除功能
    @PostMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody Integer[] ids) {

        if(ids==null ||ids.length==0)
            return false;
//        System.out.println(ids[0]);
//        return true;

        return studentService.deleteById(Arrays.asList(ids));
    }

    //上传
    @PostMapping(value="upload")
    @ResponseBody
    public  Object imporCourse(@RequestParam("file") MultipartFile file, HttpSession httpSession) throws IOException {
        CommonReslut<String> result = new CommonReslut<>();
        InputStream in = file.getInputStream();
        String fileOriginalName = file.getOriginalFilename();
        String fileName = file.getName();
        String excelType = fileOriginalName.substring(fileOriginalName.indexOf(".") + 1);

        //记录插入多条记录到了数据库；
        int importSuccessNum = 0;
        int importAllNum = 0;
        boolean importSuccess;

        System.out.println("in:::" + in);
        System.out.println("fileOriginalName:::" + fileOriginalName);
        System.out.println("fileName:::" + fileName);
        System.out.println("excelType:::" + excelType);
        List<Object> forlist = ExcelForList.ExcelForList(file, Student.class, true, excelType);
        for (Object object : forlist) {
            Student student = (Student) object;
            importAllNum++;
            importSuccess = studentService.importExcel(student)>0;
            if (importSuccess)
                importSuccessNum++;

        }
        httpSession.setAttribute("importAllNum", importAllNum);
        httpSession.setAttribute("importSuccessNum", importSuccessNum);

        if (importSuccessNum <= 0)
            result.setMsg("上传excel文件出错！");
//            return "success!"+"应处理"+importAllNum+"条,已成功处理"+importSuccessNum+"条！";

        result.setData(file.getOriginalFilename());
        return result;

    }

}